<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\WfilmModel;

class WfilmController extends BaseController {

	protected function initobj(){
		return new WfilmModel();
	}
	
	//搜索
	public function wfilmsearch(){
		$key = I('post.key');
		$where['wfilm_title'] = array('LIKE',"%".$key."%");
		$bn = $this->initobj();
		$result = $bn->where($where)->select();
		if($result){
			foreach($result as &$v){
				$v['typename'] = $this->get_idtotypeinfo('typename',$v['type_id']);
			}
		}
		$this->assign('list',$result);
		$this->display('Wfilm/Wfilm_list');
	}

	//添加
	public function add(){
		$bn = $this->initobj();
		if(!$bn->validate()->create()){
			$this->error($bn->getError(),'',1);
		}else{
			$content 			   = html_entity_decode(I('post.Wfilm_content'));
			$bn->wfilm_description = cutArticle($content, 50);
			$bn->wfilm_content 	   = $content;
			$bn->wfilm_title 	   = I('post.wfilm_title');
			$bn->wfilm_pic 	   	   = I('post.wfilm_pic');
			$bn->wfilm_jumplink    = I('post.wfilm_jumplink');
			$bn->wfilm_videopath   = html_entity_decode(I('post.Wfilm_videopath'));
			$bn->state 	   		   = I('post.state');
			$bn->pubdate 	   	   = I('post.pubdate');
			$bn->wfilm_pv 	   	   = I('post.wfilm_pv');
			$result = $bn->add();
			if($result){
				$this->success('添加成功');
			}else{
				$this->error('添加失败','',1);
			}
		}
	}

	//渲染视频添加页
	public function article(){
		$bn = $this->initobj();
		$maxweight = $bn::getvistimax($bn);
		$this->assign('maxweight',$maxweight);
		$alllist = $this->getalltypelist();
		$this->assign('typelist',$alllist);
		$this->display();
	}

	//获取分类名称
	protected function getalltypelist(){
		$bntype = A('Wfilmtype');
		$alllist = $bntype->getalllist();
		return $alllist;
	}

	//根据ID获取分类名称
	protected function get_idtotypeinfo($field,$id = null){
		$bntype = new \Admin\Model\WfilmtypeModel;
		$res = $bntype->find($id);
		return $res[$field];
	}


	//列表页输出
	public function wfilm_list($typeid = null){
		$bn = $this->initobj();
		if(!empty($typeid)){
			$where['type_id'] = $typeid;
		}else{
			$where = null;
		}
		$count = $bn->where($where)->count();
		//每页显示条数
		$num = 12;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $bn->where($where)->order(array('id'=>'DESC'))->limit($obj->firstRow.','.$obj->listRows)->order('id desc')->select();
		if($result){
			foreach($result as &$v){
				$v['typename'] = $this->get_idtotypeinfo('typename',$v['type_id']);
			}
		}
		$this->assign('list',$result);
		$this->assign('page',$show);
		$this->display();
	}

	//恢复
	public function renew($id = null){
		$bn = $this->initobj();
		$bn->state = 1;
		$res = $bn->where(array('id'=>$id))->save();
		if($res){
			$this->success('恢复成功');
		}
	}

	//删除
	public function delete($id = null){
		$bn = $this->initobj();
		$bn->state = 0;
		$res = $bn->where(array('id'=>$id))->save();
		if($res){
			$this->success('删除成功');
		}
	}

	//编辑页面
	public function edit($id = null){
		$bn = $this->initobj();
		$data = $bn::getnowdata($bn,$id);
		$this->assign('data',$data);
		$this->assign('edit',1);
		$alllist = $this->getalltypelist();
		$this->assign('typelist',$alllist);
		$this->display('Wfilm/article');
	}

	//保存
	public function sedit(){
		$bn = $this->initobj();
		$id = I('post.editid');
		$rules = array(
			array('wfilm_title','require','标题不能为空！'),
			array('wfilm_pic','require','缩略图不能为空！')
		);
		if(!$bn->validate($rules)->create()){
			$this->error($bn->getError(),'',1);
		}else{
			$content 			   = html_entity_decode(I('post.Wfilm_content'));
			$bn->wfilm_description = cutArticle($content, 50);
			$bn->wfilm_content 	   = $content;
			$bn->wfilm_title 	   = I('post.wfilm_title');
			$bn->wfilm_pic 	   	   = I('post.wfilm_pic');
			$bn->wfilm_jumplink    = I('post.wfilm_jumplink');
			$bn->wfilm_videopath   = html_entity_decode(I('post.Wfilm_videopath'));
			$bn->state 	   		   = I('post.state');
			$bn->pubdate 	   	   = I('post.pubdate');
			$bn->wfilm_pv 	   	   = I('post.wfilm_pv');
			$result = $bn->where(array('id'=>$id))->save();
			if($result>=0){
				$this->success('编辑成功',U('Wfilm_list'));
			}else{
				$this->error('编辑失败','',1);
			}
		}
	}


}